const express = require('express');
const router = express.Router();
const conn = require('../../db/mysql');
conn.getConnection((err, conn) => {
  if (err) {
    console.log("连接失败,失败信息为：" + err);
  }
})

/**
 * 获取所有文章类别信息
 * @summary 获取所有文章类别信息
 * @route GET /articletype
 * @group articletype
 * @returns {object} 200 - {code: 200,msg: "获取成功",type: "success",data: {}}
 * @returns {Error}  default - {code:400,msg: "获取成功",type: "fail",data:{ }}
 * @security JWT
 */
router.get('/articletype', (req, res, next) => {
  const sql = `SELECT * FROM articletype WHERE is_delete='0'`;
  let dataTemplent = {
    code: 200,
    type: "success",
    msg: "查询成功",
    data: {}
  }
  conn.query(sql, (err, result) => {
    if (err) {
      dataTemplent.type = "fail";
      dataTemplent.msg = "查询失败";
    } else {
      dataTemplent.data = result;
    }
    res.send(dataTemplent);
  });
});
/**
 * 获取所有文章类别信息（分页）
 * @summary 获取所有文章类别信息（分页）
 * @route GET /articletypes
 * @group articletype
 * @param {string} articletypeName.query - 分类名
 * @param {number} pageSize.query.required - 分页大小
 * @param {number} page.query.required - 页码
 * @returns {object} 200 - {code: 200,msg: "获取成功",type: "success",data: {}}
 * @returns {Error}  default - {code:400,msg: "获取成功",type: "fail",data:{ }}
 * @security JWT
 */
router.get('/articletypes', (req, res, next) => {
  let articletypeName = req.query.articletypeName||'';
  let pageSize = req.query.pageSize; // 页大小
  let pageCurrent = req.query.page; // 当前页
  let start = (pageCurrent - 1) * pageSize; // 起始位置
  let sql = "";
  if (articletypeName) {
    sql = `SELECT *  FROM articletype WHERE is_delete='0' and type_name LIKE '%${articletypeName}%' LIMIT ${start},${pageSize}`;
  } else {
    sql = `SELECT *  FROM articletype WHERE is_delete='0' LIMIT ${start},${pageSize}`;
  }
  let dataTemplent = {
    code: 200,
    msg: "查询成功",
    type: "success",
    data: {}
  }
  conn.query(sql, (err, result) => {
    if (err) throw err;
    let list = result
    conn.query(`SELECT COUNT(*) AS total FROM articletype WHERE is_delete='0' and type_name LIKE '%${articletypeName}%'`, (err, result1) => {
      if (err) {
        dataTemplent.code = 400;
        dataTemplent.type = "fail";
        dataTemplent.msg = "查询失败";
      }
      else {
        let obj = {
          pageSize: pageSize,
          pageCurrent: pageCurrent,
          total: result1[0].total,
          list: list
        }
        dataTemplent.data = obj;
      }
      res.send(dataTemplent);
    })
  })
});
/**
 * 新增文章类别
 * @summary 新增文章类别
 * @route POST /articletype
 * @group articletype
 * @param {string} articletypeInfo.body.required - articletypeName、alias
 * @returns {object} 200 - {code: 200,msg: "新增成功",type: "success",data: {}}
 * @returns {Error}  default - {code:400,msg: "新增失败",type: "fail",data:{ }}
 * @security JWT
 */
router.post('/articletype', (req, res, next) => {
  let params = req.body;
  let dataTemplent = {
    code: 200,
    msg: "新增成功",
    type: "success",
    data: {}
  }
  if (params) {
    const sql = `INSERT INTO articletype VALUES('0','${params.articletypeName}','${params.alias}','0');`;
    conn.query(sql, (err, result) => {
      if (err) {
        dataTemplent.code = 400;
        dataTemplent.type = "fail";
        dataTemplent.msg = "新增失败";
      } else {
        dataTemplent.data = { id: result.insertId };
      }
      res.send(dataTemplent);
    });
  }
});
/**
 * 修改文章类别
 * @summary 修改文章类别
 * @route PUT /articletype
 * @group articletype
 * @param {string} articletypeInfo.body.required -id articletypeName、alias
 * @returns {object} 200 - {code: 200,msg: "修改成功",type: "success",data: {}}
 * @returns {Error}  default - {code:400,msg: "修改失败",type: "fail",data:{ }}
 * @security JWT
 */
router.put('/articletype', (req, res, next) => {
  let params = req.body;
  let dataTemplent = {
    code: 200,
    type: "success",
    msg: "修改成功",
    data: {}
  }
  if (params) {
    const sql = `UPDATE articletype set type_name='${params.articletypeName}',alias='${params.alias}' WHERE type_id='${params.id}';`;
    conn.query(sql, (err, result) => {
      if (err) {
        dataTemplent.code = 400;
        dataTemplent.type = "fail";
        dataTemplent.msg = "修改失败";
      } else {
        dataTemplent.data = {};
      }
      res.send(dataTemplent);
    });
  }
});
/**
 * 删除文章类别
 * @summary 删除文章类别
 * @route DELETE /articletype
 * @group articletype
 * @param {string} id.body.required -文章类别id
 * @returns {object} 200 - {code: 200,msg: "删除成功",type: "success",data: {}}
 * @returns {Error}  default - {code:500,msg: "删除失败",type: "fail",data:{ }}
 * @security JWT
 */
router.delete('/articletype', (req, res, next) => {
  let params = req.body;
  let dataTemplent = {
    code: 200,
    type: "success",
    msg: "删除成功",
    data: {}
  }
  if (params) {
    const sql = `UPDATE articletype set is_delete='1' WHERE type_id='${params.id}';`;
    conn.query(sql, (err, result) => {
      if (err) {
        dataTemplent.code = 400;
        dataTemplent.type = "fail";
        dataTemplent.msg = "删除失败";
      } else {
        dataTemplent.data = { affectedRows: result.affectedRows };
      }
      res.send(dataTemplent);
    });
  }
});
module.exports = router;